const App = getApp()
import {
  OrderModel
} from '../../../../models/order'
const orderModel = new OrderModel()
import { encodeUnicode } from '../../../../utils/index.js'


Page({

    /**
     * 页面的初始数据
     */
    data: {  
      evaluateGoodsList: [],
      goodsModel:{},
      urlName:'weapp/comments/',
      height:'',
      have:true,
      statusBarHeight: App.globalData.statusBarHeight,
      isTabbar:false
    },
  
    //获取输入字数
    bindWordLimit: function (e) {
      const index = e.currentTarget.dataset.index;
      this.data.evaluateGoodsList[index].wordNum = e.detail.value.length;
      this.data.evaluateGoodsList[index].desc = encodeUnicode(e.detail.value);
      this.setData({
        evaluateGoodsList: this.data.evaluateGoodsList
      })
    },
    
    //获取售后商品信息
    evaluateGoods: function (e) {
      const self = this;
      //判断是单个评价还是批量评价
      if(e.evaluateNum == 1){
        //单个评价
        const params = {
          orderId: e.orderId,
          orderGoodsId: e.orderGoodsId
          // orderId: 'ae92e6cbea0747d8aa49b6192982b601',
          // orderGoodsId:'95cf84487aa04145aaf484f3bfcc7ffb'
        }
        orderModel.evaluateGoods(params).then(res => {
          if(res.data.evaluateGoods){
              self.data.evaluateGoodsList.push(res.data.evaluateGoods)
            for (var i in self.data.evaluateGoodsList) {
              self.data.evaluateGoodsList[i].wordNum = 0
              self.data.evaluateGoodsList[i].desc = ''
              self.data.evaluateGoodsList[i].Imglist = []
            }
            self.setData({
              evaluateGoodsList: self.data.evaluateGoodsList
            })
          }else{
            self.setData({
              have:false
            })
          }
          
        }).catch();
      }else{
        //批量评价
        const params = {
          orderId: e.orderId
        }
        orderModel.evaluateGoodsList(params).then(res => {
          if(res.data.evaluateGoodsList){
            for (var i in res.data.evaluateGoodsList) {
              res.data.evaluateGoodsList[i].wordNum = 0
              res.data.evaluateGoodsList[i].desc = ''
              res.data.evaluateGoodsList[i].Imglist = []
            }
            self.setData({
              evaluateGoodsList: res.data.evaluateGoodsList
            })
          }else{
            self.setData({
              have: false
            })
          }
          
        }).catch();
      }
      
    },
    
    //获取上传的图片信息
    ImgUpload: function (e) {
      const index = e.currentTarget.dataset.index;
      this.data.evaluateGoodsList[index].Imglist = e.detail
      this.setData({
        evaluateGoodsList: this.data.evaluateGoodsList
      })
    },
    
    //提交售后
    release: function (e) {
      const self = this;
      const evaluateGoodsList = self.data.evaluateGoodsList;
      const evaluateNum = self.data.goodsModel.evaluateNum;
      const pingjia = [];
      //判断信息是否为空
      for (var i in evaluateGoodsList) {
        const object = {
          orderId: evaluateGoodsList[i].orderId,
          orderGoodsId: evaluateGoodsList[i].id,
          evaluateDes: evaluateGoodsList[i].desc
        };
        //问题描述判断
        if (!object.evaluateDes) {
          //批量评价
          if(evaluateNum == 2){
            wx.showToast({
              title: '第' + (Number(i) + 1) + '款商品的分享不能为空',
              icon: 'none'
            })
          }else{
            //单个评价
            wx.showToast({
              title: '分享不能为空',
              icon: 'none'
            })
          }
          return;
        }

        //上传图片为空判断
        if (evaluateGoodsList[i].Imglist.length < 1) {
          //批量评价
          if(evaluateNum == 2){
            App.WxService.showToast({
              title: '第' + (Number(i) + 1) + '款商品请上传照片',
              icon: 'none'
            });
          }else{
            //单个评价
            App.WxService.showToast({
              title: '请上传照片',
              icon: 'none'
            });
          }
          
          return;
        }

        const len = evaluateGoodsList[i].Imglist;
        const imgUrl = [];
        if (self.data.goodsModel.evaluateNum == 1) {
          for (var i = 0; i < len.length; i++) {
            const A= {};
            A.imgUrl = len[i];
            imgUrl.push(A);
          }
        } else {
          for (var i = 0; i < len.length; i++) {
            imgUrl.push(len[i]);
          }
        }
        object.evaluateImgList = imgUrl;
        pingjia.push(object);
      }
      //单个评价
      if(self.data.goodsModel.evaluateNum == 1){
        const params = {
          orderId: pingjia[0].orderId,
          orderGoodsId: pingjia[0].orderGoodsId,
          evaluateDes: pingjia[0].evaluateDes,
          evaluateImgList: pingjia[0].evaluateImgList
        }
        orderModel.addEvaluate(params).then(res => {
          App.WxService.showToast({
            title: '评论成功',
            icon: 'none'
          })
          wx.redirectTo({
            url: '/pages/account/order/comment/success/index?gold=' + res.data.gold
          })
        })
      }else{
        //批量评价
        const params = {
          pingjia: pingjia
        }
        orderModel.addAllEvaluate(params).then(res => {
          if (res.data.num == params.pingjia.length){
            App.WxService.showToast({
              title: '评论成功',
              icon: 'none'
            })
            wx.redirectTo({
              url: '/pages/account/order/comment/success/index?gold=' + res.data.gold
            })
          }else{
            App.WxService.showToast({
              title: '评论失败',
              icon: 'none'
            })
            return;
          }
          
        })
      }

      
      
    },
    
  
    /**
     * 生命周期函数--监听页面加载
     */
  onLoad: function (options) {
      wx.getSystemInfo({
        success: (res) => {
          this.setData({
            height: res.windowHeight
          })
        }
      })
      this.setData({
        goodsModel:options
      })
      this.evaluateGoods(options);
    },
  
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
      
    },
  
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
      
    },
  
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {
      
    },
  
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
      
    },
  
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
      
    },
  
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
      
    },

  })